------Insert TK tiet kiem-----------
if exists (select name from sysobjects where name like 'Proc_TKTietKiem_Insert')
	drop procedure Proc_TKTietKiem_Insert
go

create PROC Proc_TKTietKiem_Insert (@MaTKChinh int, @sotien float,@MaChiTietLS int,@MaLoaiTK int, @ngaygui datetime, @ngaydaohan datetime, @type int)
as
begin
	if(@type=1)--dirty
	begin
		begin tran
			insert into TaiKhoanTietKiem(MaTKChinh, SoTien, MaChiTietLS, NgayGui, NgayDaoHan, DelFlg, MaLoaiTK)
			values( @MaTKChinh, @sotien, @MaChiTietLS, @ngaygui, @ngaydaohan, 0,@MaLoaiTK)
		waitfor delay '00:00:05'
		rollback tran
	end
	if(@type=2)--phantom
	begin
		begin tran
			insert into TaiKhoanTietKiem(MaTKChinh, SoTien, MaChiTietLS, NgayGui, NgayDaoHan, DelFlg, MaLoaiTK)
			values( @MaTKChinh, @sotien, @MaChiTietLS, @ngaygui, @ngaydaohan, 0,@MaLoaiTK)
		commit tran
	end
end
go

-----------update ngay gui va ngay dao han --------------
if exists (select name from sysobjects where name like 'Proc_TKTietKiem_UpdateNgay')
	drop procedure Proc_TKTietKiem_UpdateNgay
go

create PROC Proc_TKTietKiem_UpdateNgay (@MaTkTietKiem int, @sotien float, @ngaygui datetime, @ngaydaohan datetime, @type int)
as
begin
	if(@type=1)--dirty
	begin
		begin tran
			update TaiKhoanTietKiem set SoTien=@sotien, NgayGui=@ngaygui, NgayDaoHan=@ngaydaohan
			where MaTkTietKiem = @MaTkTietKiem
		waitfor delay '00:00:05'
		rollback tran
	end
	if(@type=3)--unrepeatable
	begin
		begin tran
			update TaiKhoanTietKiem set SoTien=@sotien, NgayGui=@ngaygui, NgayDaoHan=@ngaydaohan
			where MaTkTietKiem = @MaTkTietKiem
		commit tran
	end
end
go

-----------Rut So Tiet Kiem-------
if exists (select name from sysobjects where name like 'Proc_TKTietKiem_RutSoTK')
	drop procedure Proc_TKTietKiem_RutSoTK
go

create PROC Proc_TKTietKiem_RutSoTK (@MaTkTietKiem int,@DelFlg int, @type int)
as
begin
	if(@type=1)--dirty
	begin
		begin tran
			update TaiKhoanTietKiem set DelFlg=@DelFlg
			where MaTkTietKiem = @MaTkTietKiem
		waitfor delay '00:00:05'
		rollback tran
	end
	if(@type=3)--unrepeatable
	begin
		begin tran
			update TaiKhoanTietKiem set DelFlg=@DelFlg
			where MaTkTietKiem = @MaTkTietKiem
		commit tran
	end
end
go


------------------xem thong tin tai khoan tiet kiem  qua ma tai khoan chinh ------------------
if exists (select name from sysobjects where name like 'Proc_TaiKhoanTietKiem_GetByMaTKChinh')
	drop procedure Proc_TaiKhoanTietKiem_GetByMaTKChinh
go

create PROC Proc_TaiKhoanTietKiem_GetByMaTKChinh (@maTKChinh int,@type int)
as
begin
	if(@type=1)--dirty
	begin
		begin tran
		set tran isolation level read uncommitted
			select * from TaiKhoanTietKiem
			where MaTKChinh = @maTKChinh and DelFlg=0
		commit tran
	end
	else if(@type=2)--phantom
	begin
		begin tran
		set transaction isolation level repeatable read
			select * from TaiKhoanTietKiem	where MaTKChinh = @maTKChinh and DelFlg=0
			waitfor delay '00:00:05'
			select * from TaiKhoanTietKiem	where MaTKChinh = @maTKChinh and DelFlg=0
		commit tran
	end
	else if(@type=3)--unrepeatable
	begin
		begin tran
			waitfor delay '00:00:05'
			select * from TaiKhoanTietKiem
			where MaTKChinh = @maTKChinh and DelFlg=0
		commit tran
	end
end
go
--Others
------------------xem Danh Sach Ky Han ------------------
if exists (select name from sysobjects where name like 'Proc_KyHan_All')
	drop procedure Proc_KyHan_All
go

create PROC Proc_KyHan_All
as
	select * from LoaiKyHanLS
	where DelFlg=0
go

